package common

import "errors"

type Undo []func()

func (u *Undo)Add(f func())  {
	*u = append(*u, f)
}

//调一次，就执行最末尾的的一个函数
func (u *Undo)Undo() error {
	functions := *u
	if len(functions) == 0 { return errors.New("No function to undo") }
	index := len(functions) -1
	if function := functions[index]; function != nil {
		function()
		functions[index] = nil
	}
	*u = functions[:index]
	return nil
}